/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} targetSum
 * @return {boolean}
 */
export var hasPathSum = function (root, targetSum) {
  if (!root) return false
  return dfs(root, targetSum)
}

function dfs(node, targetSum, sum = 0) {
  sum = node.val + sum
  if (!node.left && !node.right) return sum === targetSum
  if (node.left && dfs(node.left, targetSum, sum)) return true
  if (node.right && dfs(node.right, targetSum, sum)) return true
  return false
}
